import axios from 'axios'
import config from './configEnv/index'
import Vue from 'vue'

// axios.defaults.timeout = 100000;

const instance = axios.create();
// const BASEURL = 'http://192.168.5.81:8000/';
// const BASEURL = window.location.protocol+'//'+window.location.hostname+':8000/';
instance.defaults.baseURL = config.api.allurl;
// http request 拦截器
instance.interceptors.request.use(config => {
  const authToken = JSON.parse(localStorage.getItem('authToken'));
  config.headers['Authorization'] = 'Bearer ' + authToken;
  return config;
}, error => {
  console.log(error)
})

/**
 * 封装get方法
 * @param url  请求url
 * @param params  请求参数
 * @returns {Promise}
 */
export function get(url, params = {}, config={}) {
  return new Promise((resolve, reject) => {
    const paramsObj = { params: params, ...config };
    instance.get(url, paramsObj).then((response) => {
      resolve(response);
    })
      .catch((error) => {
        reject(error);
      });
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data={}, config={}) {
  return new Promise((resolve, reject) => {
    instance.post(url, data, config).then(
      (response) => {
        //关闭进度条
        resolve(response);
      },
      (err) => {
        msag(err);
        reject(err);
      }
    );
  });
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url, data = {}) {
  return new Promise((resolve, reject) => {
    instance.patch(url, data).then(
      (response) => {
        resolve(response);
      },
      (err) => {
        msag(err);
        reject(err);
      }
    );
  });
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    instance.put(url, data).then(
      (response) => {
        resolve(response);
      },
      (err) => {
        msag(err);
        reject(err);
      }
    );
  });
}
/**
 * 封装delete请求
 * @param url
 * @param params
 * @returns {Promise}
 */
export function DELETE(url, params = {}) {
  return new Promise((resolve, reject) => {
    instance.delete(url, { data: params }).then(
      (response) => {
        resolve(response);
      },
      (err) => {
        msag(err);
        reject(err);
      }
    );
  });
}
//失败提示
function msag(err) {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        alert(err.response.data.error.details);
        break;
      case 401:
        alert("未授权，请登录");
        break;

      case 403:
        alert("拒绝访问");
        break;

      case 404:
        alert("请求地址出错");
        break;

      case 408:
        alert("请求超时");
        break;

      case 500:
        Vue.prototype.$message.error('后台响应失败，请联系管理员!');
        // alert("服务器内部错误");
        break;

      case 501:
        alert("服务未实现");
        break;

      case 502:
        alert("网关错误");
        break;

      case 503:
        alert("服务不可用");
        break;

      case 504:
        alert("网关超时");
        break;

      case 505:
        alert("HTTP版本不受支持");
        break;
      default:
    }
  }
}

//统一接口处理，返回数据
export default function(fecth, url, param, config={}) {
  return new Promise((resolve, reject) => {
    switch (fecth) {
      case "get":
        get(url, param, config)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            reject(error);
          });
        break;
      case "post":
        post(url, param, config)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            reject(error);
          });
        break;
      case "put":
        put(url, param)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            reject(error);
          });
        break;
      case "patch":
        patch(url, param)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            reject(error);
          });
        break;
      case "DELETE":
        DELETE(url, param)
          .then(function (response) {
            resolve(response);
          })
          .catch(function (error) {
            reject(error);
          });
        break;
      default:
        break;
    }
  });
}